package com.lsyw.intergrate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsyw.intergrate.common.Result;
import com.lsyw.intergrate.entity.JcSdPlan;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.lsyw.intergrate.service.JcSdPlanService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 手动监测计划 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2024-01-03
 */
@Api(tags = "手动监测计划")
@RestController
@RequestMapping("/jc-sd-plan")
public class JcSdPlanController {


    @Autowired
    private JcSdPlanService jcSdPlanService;


    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id")
    public Result getById(String id) {
        return Result.successResult(jcSdPlanService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    public Result save(@RequestBody JcSdPlan jcSdPlan) {
        jcSdPlanService.save(jcSdPlan);
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id/{id}")
    public Result delete(@PathVariable(value = "id") String ids) {
        String[] idsStrs = ids.split(",");
        for (String id : idsStrs) {
            jcSdPlanService.removeById(Long.parseLong(id));
        }
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result updateById(@RequestBody JcSdPlan jcSdPlan) {
        jcSdPlanService.updateById(jcSdPlan);
        return Result.successResult(null);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    public Result list(@RequestBody JcSdPlanReqVo jcSdPlan) {
        final LambdaQueryWrapper<JcSdPlan> lambda = new QueryWrapper<JcSdPlan>().lambda();
        this.buildCondition(lambda, jcSdPlan);
        return Result.successResult(jcSdPlanService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<JcSdPlan> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        final LambdaQueryWrapper<JcSdPlan> lambda = new QueryWrapper<JcSdPlan>().lambda();
        //TODO 自己的查询逻辑
        return Result.successResult(this.jcSdPlanService.page(page, lambda));
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<JcSdPlan> lambda, JcSdPlanReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(JcSdPlan::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getName())) {
            lambda.eq(JcSdPlan::getName, param.getName());
        }
        if (!StringUtils.isEmpty(param.getHoleIds())) {
            lambda.eq(JcSdPlan::getHoleIds, param.getHoleIds());
        }
        if (!CollectionUtils.isEmpty(param.getPlanTimeStartList())) {
            lambda.ge(JcSdPlan::getPlanTimeStart, param.getPlanTimeStartList().get(0));
            lambda.le(JcSdPlan::getPlanTimeStart, param.getPlanTimeStartList().get(1));
        }
        if (!StringUtils.isEmpty(param.getWeight())) {
            lambda.eq(JcSdPlan::getWeight, param.getWeight());
        }
        if (!StringUtils.isEmpty(param.getJcType())) {
            lambda.eq(JcSdPlan::getJcType, param.getJcType());
        }
        if (!StringUtils.isEmpty(param.getUserId())) {
            lambda.eq(JcSdPlan::getUserId, param.getUserId());
        }
        if (!StringUtils.isEmpty(param.getMassifId())) {
            lambda.eq(JcSdPlan::getMassifId, param.getMassifId());
        }
        if (!StringUtils.isEmpty(param.getGmtCreateUser())) {
            lambda.eq(JcSdPlan::getGmtCreateUser, param.getGmtCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtCreateList())) {
            lambda.ge(JcSdPlan::getGmtCreate, param.getGmtCreateList().get(0));
            lambda.le(JcSdPlan::getGmtCreate, param.getGmtCreateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getGmtModifiedUser())) {
            lambda.eq(JcSdPlan::getGmtModifiedUser, param.getGmtModifiedUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtModifiedList())) {
            lambda.ge(JcSdPlan::getGmtModified, param.getGmtModifiedList().get(0));
            lambda.le(JcSdPlan::getGmtModified, param.getGmtModifiedList().get(1));
        }
        if (!StringUtils.isEmpty(param.getDelFlag())) {
            lambda.eq(JcSdPlan::getDelFlag, param.getDelFlag());
        }
        if (!CollectionUtils.isEmpty(param.getPlanTimeEndList())) {
            lambda.ge(JcSdPlan::getPlanTimeEnd, param.getPlanTimeEndList().get(0));
            lambda.le(JcSdPlan::getPlanTimeEnd, param.getPlanTimeEndList().get(1));
        }
        if (!StringUtils.isEmpty(param.getUnitMeasurement())) {
            lambda.eq(JcSdPlan::getUnitMeasurement, param.getUnitMeasurement());
        }
        lambda.orderBy(true, false, JcSdPlan::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class JcSdPlanReqVo extends JcSdPlan {
        private List<String> planTimeStartList; // 计划时间开始起止
        private List<String> gmtCreateList; // 创建时间起止
        private List<String> gmtModifiedList; // 更新时间起止
        private List<String> planTimeEndList; // 计划时间开始起止
    }


}
